# -*- coding: utf-8 -*-
# @Time    : 2024/6/24 9:58
# @Author  : floraachy
# @File    : repo_issue_detail_page
# @Software: PyCharm
# @Desc:

# 标准库导入
import os
from typing import Optional, Union, List
# 第三方库导入
import allure
# 本地应用/模块导入
from utils.base_utils.base_page import BasePage


class RepoIssueDetailPage(BasePage):
    """项目-X疑修详情页"""
    # 编辑 按钮
    locator_edit_btn = "//a[contains(text(), '编辑')]"
    # 复制 按钮
    locator_copy_btn = "//a[contains(text(), '复制')]"
    # 删除按钮
    locator_delete_btn = "//a[text()='删除']"
    # 附件
    locator_file_text = "//div[contains(@class, 'attachment-list-div')]//span[1]"
    # 删除疑修二次确认弹窗弹出
    locator_delete_confirm_pop = "//p[text()='{}']"
    # 确认删除 按钮
    locator_confirm_delete_btn = "//span[text()='确认删除']/parent::button"

    # 疑修标题 文本
    locator_issue_title_text = "//div[@class='detailtitle']/div/p"
    # 疑修内容文本
    locator_issue_desc_text = "//div[@class='descPanel']/div/p"
    # 疑修附件名称
    locator_issue_file_text = "//div[contains(@class, 'attachment-list-div')]//span[@class='ml20'][1]/preceding-sibling::span"

    # 疑修标题
    locator_issue_title_input = "id=subject"
    # 疑修内容
    locator_issue_content_input = "//textarea/following::div[contains(@class, 'CodeMirror-wrap')]"
    # 疑修附件
    locator_issue_file_input = "//p[text()='拖动文件或点击此处上传']/parent::div/preceding-sibling::input"
    # 保存 按钮
    locator_save_btn = "//span[contains(text(), '保 存')]/.."
    # 疑修附件的删除图标
    locator_issue_file_delete_icon = "//span[contains(text(),'{}')]/parent::a/following-sibling::span/i"
    # 是 按钮
    locator_yes_btn = "//span[text()='是']"

    @allure.step("访问疑修详情页面：/{repo_owner}/{repo_identifier}/issues/{issue_index}")
    def navigate(self, repo_owner, repo_identifier, issue_index):
        """
        访问疑修详情页面
        """
        self.visit(f"/{repo_owner}/{repo_identifier}/issues/{issue_index}")

    @allure.step("疑修详情，点击【编辑】按钮，进入编辑疑修页面")
    def click_edit_issue_button(self):
        """
        点击'编辑'按钮，进入编辑疑修页面
        """
        self.click(locator=self.locator_edit_btn)

    @allure.step("疑修详情，点击【复制】按钮，进入复制疑修页面")
    def click_copy_issue_button(self):
        """
        点击'复制'按钮，进入复制疑修页面
        """
        self.click(locator=self.locator_copy_btn)

    @allure.step("疑修详情，点击【删除】按钮，进行删除疑修操作")
    def click_delete_issue_button(self):
        """
        疑修详情，点击'删除'按钮
        """
        self.click(self.locator_delete_btn)
        self.page.wait_for_timeout(3000)

    @allure.step("删除疑修确认弹窗，点击【确认删除】按钮")
    def click_confirm_delete_button(self):
        """
         点击'确认删除'按钮
        """
        self.click(self.locator_confirm_delete_btn)
        self.page.wait_for_timeout(3000)

    @allure.step("获取疑修所有附件")
    def get_issue_attachments(self):
        """
        获取疑修所有附件
        """
        return self.get_all_elements_text(locator=self.locator_file_text)

    @allure.step("进入编辑疑修页面：/{repo_owner}/{repo_identifier}/issues/{issue_index}")
    def navigate(self, repo_owner, repo_identifier, issue_index):
        """
        进入编辑疑修页面
        """
        self.visit(f"/{repo_owner}/{repo_identifier}/issues/{issue_index}")
        self.page.wait_for_timeout(1000)

    @allure.step("输入疑修标题: {issue_title}")
    def input_issue_title(self, issue_title: str):
        """
        输入疑修标题
        """
        self.page.wait_for_timeout(300)  # 超时时间是0.3秒
        self.input(locator=self.locator_issue_title_input, text=issue_title)

    @allure.step("输入疑修内容: {issue_desc}")
    def input_issue_desc(self, issue_desc: str):
        """
        输入疑修内容
        """
        # todo 疑惑：这里必须加wait_for_timeout，否则即使用的wait_for_selector，也无法输入内容。
        self.page.wait_for_timeout(2000)
        # elem = page.wait_for_selector(
        #     selector="//textarea/following::div[contains(@class, 'CodeMirror-wrap')]", timeout=10000, state='visible')
        elem = self.page.query_selector(selector=self.locator_issue_content_input)
        self.execute_js('''([elem, content]) => elem.CodeMirror.setValue(content);''',
                        [elem, issue_desc])

    @allure.step("上传疑修附件: {issue_file}")
    def upload_issue_attachments(self, issue_file: str):
        """
        上传疑修附件
        """
        self.upload_file(locator=self.locator_issue_file_input, file_path=issue_file)

    @allure.step("点击文件：{file_name} 对应的删除图标")
    def click_delete_attach_icon(self, file_name):
        """
        点击疑修删除附件图标
        """
        # 根据文件名称寻找对应文件的删除图标
        file = os.path.basename(file_name) if os.path.isabs(file_name) else file_name
        locator_issue_file_delete_icon = self.locator_issue_file_delete_icon.format(file)
        self.click(locator_issue_file_delete_icon)

    @allure.step("删除疑修附件确认弹窗，点击【是】按钮")
    def click_yes_button(self):
        """
        删除疑修附件确认弹窗，点击"是”按钮
        """
        self.click(self.locator_yes_btn)
        self.page.wait_for_timeout(1000)

    @allure.step("点击【保存】按钮，提交表单校验，进行疑修更新")
    def click_save_issue_button(self):
        """
        点击'保存'按钮，提交表单校验，进行疑修更新
        """
        self.click(self.locator_save_btn)
        self.page.wait_for_timeout(3000)

    @allure.step("删除疑修流程")
    def delete_issue_flow(self):
        """
        疑修详情页，删除疑修
        """
        # 点击【删除】按钮
        self.click_delete_issue_button()
        # 点击【确认删除】按钮
        self.click_confirm_delete_button()

    # --------------------- 流程 -------------------------------------
    @allure.step(
        "编辑疑修流程，标题：{issue_title}， 内容：{issue_desc}， 删除的附件：{delete_issue_file}， 上传的附件： {upload_issue_file}")
    def edit_issue_flow(self, issue_title: str, issue_desc: Optional[str] = None,
                        delete_issue_file: Optional[Union[str, List[str]]] = None,
                        upload_issue_file: Optional[Union[str, List[str]]] = None) -> None:
        """
        详情页，编辑疑修流程：
        1. 修改疑修标题(必填)
        2. 修改疑修内容(选填)
        3. 删除疑修附件(选填)
        4. 上传疑修附件
        5. 点击"保存"按钮
        """
        if issue_title:
            self.input_issue_title(issue_title)
        if issue_desc:
            self.input_issue_desc(issue_desc)
        if delete_issue_file:
            if isinstance(delete_issue_file, list):
                for file in delete_issue_file:
                    self.click_delete_attach_icon(file)
                    self.click_yes_button()
            if isinstance(delete_issue_file, str):
                self.click_delete_attach_icon(file_name=delete_issue_file)
        if upload_issue_file:
            if isinstance(upload_issue_file, list):
                for file in upload_issue_file:
                    self.upload_issue_attachments(file)
            if isinstance(upload_issue_file, str):
                self.upload_issue_attachments(upload_issue_file)
        self.click_save_issue_button()
